MobX Quick Start Guide: Supercharge the client state in your React apps with MobX by Pavan Podila & Michel Weststrate

MobX Quick Start Guide: Supercharge the client state in your React apps with MobX by Pavan Podila & Michel Weststrate

Author:Pavan Podila & Michel Weststrate [Podila, Pavan]
Language: eng
Format: epub
Tags: COM051260 - COMPUTERS / Programming Languages / JavaScript, COM060180 - COMPUTERS / Web / Web Services and APIs, COM060160 - COMPUTERS / Web / Web Programming
Publisher: Packt Publishing
Published: 2018-07-25T23:00:00+00:00


this.loginState = 'completed';

yield this.sendAnalytics();

this.loginState = 'reported';

yield this.delay(3000);

});

}

new AuthStore().login();

Notice the use of the generator function*() instead of the regular function, which is passed as argument to flow(). Structurally, it is no different than the async-await style of code, but has the added benefit of automatically wrapping the parts of code following a yield with an action(). With flow(), you are back to being more declarative with your async code.

There is yet another benefit that flow() gives you. It is the ability to cancel execution of the async code.

The return value of flow() is a function that you can invoke to execute the async code. This is the login method of the AuthStore, in the preceding example. When you call new AuthStore().login(), you get back a promise that has been enhanced by MobX with the cancel() method:

const promise = new AuthStore().login2();

promise.cancel(); // prematurely cancel the async code

This is useful for canceling a long-running operation by giving user-level control.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.